package com.normalsys.live.base.util.project;

import com.normalsys.live.base.config.Constant;

import java.math.BigDecimal;

/**
 * Created with IntelliJ IDEA.
 * User: 刘天宇
 * Date: 2018/7/22
 * Time: 10:52
 * Description: 工具类
 */
public class BMIUtil {

    /**
     * 获得速度
     * @param distance
     * @param time
     * @return
     */
    public static String getAvgSpeed(String distance, String time) {
        Double disDecimal = new BigDecimal(distance).doubleValue();
        Double timeDecimal = new BigDecimal(time).doubleValue();
        Double result = disDecimal/timeDecimal;
        String first = new BigDecimal(result).setScale(0, BigDecimal.ROUND_DOWN).toString();
        String end = new BigDecimal((result-Integer.valueOf(first))*60).setScale(0, BigDecimal.ROUND_DOWN).toString();

        return first+"'"+end+"''";

    }

    /**
     * 获取bmi
     * @param height  身高
     * @param weight  体重
     * @return
     */
    public static String getBMI(String height, String weight) {
        BigDecimal up = new BigDecimal(weight);
        BigDecimal down = new BigDecimal(height).multiply(new BigDecimal(height)).divide(new BigDecimal(10000));
        BigDecimal result = up.divide(down, 1, BigDecimal.ROUND_HALF_UP);
        return result.toString();
    }

    /**
     * 获得距离
     * @param height  身高
     * @param gender  性别
     * @param count   步数
     * @return
     */
    public static String getDistance(String height, Integer gender, Integer count) {
        BigDecimal first = new BigDecimal(height).divide(new BigDecimal(100));
        BigDecimal distance;
        BigDecimal result;
        if(1 == gender) {
            distance = new BigDecimal(Constant.WOMAN_PARAM).multiply(first);
        } else {
            distance = new BigDecimal(Constant.MAN_PARAM).multiply(first);
        }
        result = distance.multiply(new BigDecimal(count));
        return result.toString();
    }

    /**
     * 获得卡路里
     * @param weight  体重
     * @param distance 距离
     * @return
     */
    public static String getCalorie(String weight, String distance) {
        BigDecimal first = new BigDecimal(weight);
        BigDecimal result = first.multiply(new BigDecimal(distance)).multiply(new BigDecimal(Constant.CALORIE_PARAM));
        return result.toString();
    }

    /**
     * 获得速度
     * @param distance 距离
     * @param time     时间
     * @return
     */
    public static String getSpeed(String distance, String time) {
        return new BigDecimal(distance).divide(new BigDecimal(time), 1, BigDecimal.ROUND_HALF_UP).toString();
    }

    /**
     * 获得类型
     * @param height
     * @param step
     * @param gender
     * @return
     */
    public static Integer getType(String height, Integer gender, Integer step, String unit) {
        String distance = getDistance(height,gender,step);
        if("1".equals(unit)) {//小时
            String speed = getSpeed(new BigDecimal(distance).multiply(new BigDecimal(1000)).toString(),"1");//km/h
            return getType(speed);
        }
        //分钟
        String speed = getSpeed(new BigDecimal(distance).divide(new BigDecimal("16.66"), 2, BigDecimal.ROUND_HALF_DOWN).toString(),"1");//km/h
        return getType(speed);
    }

    /**
     * 获得卡路里
     * @param height
     * @param step
     * @param gender
     * @return
     */
    public static String getCalorie(String height, String weight, Integer gender, Integer step) {
        String distance = getDistance(height,gender,step);
        return getCalorie(weight, new BigDecimal(distance).divide(new BigDecimal(1000)).toString());
    }
    /**
     * 获得类型
     * @param speed
     * @return
     */
    public static Integer getType(String speed) {
        Double sp = Double.valueOf(speed);
        if(sp < 4.5) {
            return 5;//慢跑
        }
        if(sp < 6) {
            return 4;//步行
        }
        if(sp < 7.5) {
            return 3;//快走
        }
        if(sp < 8) {
            return 2;//慢跑
        }
        return 1;//快跑

    }
}
